//
//  SearchViewController.m
//  LimitFree
//
//  Created by LZXuan on 15-6-26.
//  Copyright (c) 2015年 轩哥. All rights reserved.
//

#import "SearchViewController.h"

@interface SearchViewController ()

@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //修改父类的一些设置
    //导航条不需要左右按钮
    self.navigationItem.leftBarButtonItem = nil;
    self.navigationItem.rightBarButtonItem = nil;
    //父类 的tableView 减-49 SearchViewController隐藏了tabBar
    CGRect frame = self.tableView.frame;
    frame.size.height += 49;
    self.tableView.frame = frame;//当前界面 要 多+49
    //不需要搜索条
    self.tableView.tableHeaderView = nil;
    //标题
    [self addTitleViewWithTitle:@"搜索结果"];
    
    [self firstDownload];
    [self creatRefreshView];
}
//重写
- (void)firstDownload {
    self.categoryId = @"0";
    self.currentPage = 1;
    NSString *url = [NSString  stringWithFormat:self.searchUrl,self.currentPage,self.keyword];
    //关键字 有可能是汉字 (在url 中汉字是非法字符)
    
    url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSLog(@"url:%@",url);
    
    //增加下载任务
    [self addTaskWithUrl:url isRefresh:NO];
}

//刷新   子类调用
- (void)creatRefreshView {
    //增加下拉刷新
    
    //下面使用block 如果内部对self 进行操作 会存在 两个强引用 这样两个对象都不会释放导致内存泄露 (或者死锁就是两个对象不释放)
    //只要出现了循环引用 必须一强一弱 这样用完之后才会释放
    //arc 用 __weak  mrc __block
    
    __weak typeof (self) weakSelf = self;//弱引用
    
    [self.tableView addRefreshHeaderViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        //重新下载数据
        if (weakSelf.isRefreshing) {
            return ;
        }
        weakSelf.isRefreshing = YES;//标记正在刷新
        weakSelf.currentPage = 1;
        
        NSString *url = [NSString  stringWithFormat:weakSelf.searchUrl,weakSelf.currentPage,weakSelf.keyword];
        //关键字 有可能是汉字 (在url 中汉字是非法字符)
        
        url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
        [weakSelf addTaskWithUrl:url isRefresh:YES];
        
    }];
    
    //上拉加载更多
    [self.tableView addRefreshFooterViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        //重新下载数据
        if (weakSelf.isLoadMore) {
            return ;
        }
        weakSelf.isLoadMore = YES;//标记正在刷新
        weakSelf.currentPage ++;//页码加1
        
        NSString *url = [NSString  stringWithFormat:weakSelf.searchUrl,weakSelf.currentPage,weakSelf.keyword];
        //关键字 有可能是汉字 (在url 中汉字是非法字符)
        
        url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
        [weakSelf addTaskWithUrl:url isRefresh:YES];
    }];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
